//
//  MWCountryCodeController.m
//  XiRan-iOS
//
//  Created by Richfit on 2020/3/17.
//  Copyright © 2020 Wood. All rights reserved.
//

#import "MWCountryCodeController.h"
#import "MWCountryCodeCell.h"

@interface MWCountryCodeController () <UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) UITableView *tableView;

@property(nonatomic, strong) UIButton *backBtn;
@property(nonatomic, strong)NSMutableArray *countryArray;

@end

@implementation MWCountryCodeController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupPageSubviews];
    [self layoutPageSubviews];
    
    [self loadCountryCode];
}

- (void)backAction:(UIButton *)sender {
    if (_isPresent) {
        [self dismissViewControllerAnimated:YES completion:^{}];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - 请求国家码
- (void)loadCountryCode {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
    [[MWNetworkManager manager] POST:kCountryCodeUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        if (code == 1) {
            NSArray *tempArray = [MWCountryCode mj_objectArrayWithKeyValuesArray:[dic objectForKey:@"data"]];
            [self.countryArray addObjectsFromArray:tempArray];
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - UITableViewDataSource && UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.countryArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"MWCountryCodeCell";
    MWCountryCodeCell *countryCodeCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!countryCodeCell) {
        countryCodeCell = [[MWCountryCodeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        countryCodeCell.contentView.backgroundColor = MWHexColor(@"#ECF9FF");
        countryCodeCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    [countryCodeCell configData:self.countryArray[indexPath.row]];
    
    return countryCodeCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.countryCodeBlock) {
        MWCountryCode *countryCode = self.countryArray[indexPath.row];
        self.countryCodeBlock(countryCode.code);
    }
    
    if (_isPresent) {
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (NSMutableArray *)countryArray {
    if (!_countryArray) {
        _countryArray = [[NSMutableArray alloc] init];
    }
    return _countryArray;
}

- (void)setupPageSubviews {
    _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backBtn setImage:[UIImage imageNamed:@"ic_arrow_left"] forState:UIControlStateNormal];
    [_backBtn setImage:[UIImage imageNamed:@"ic_arrow_left"] forState:UIControlStateHighlighted];
    [_backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_backBtn];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = MWHexColor(@"#ECF9FF");
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    _tableView.estimatedRowHeight = 200;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
}

- (void)layoutPageSubviews {
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kStatusBarHeight + 15);
        make.left.equalTo(@16);
    }];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backBtn.mas_bottom).offset(10);
        make.left.right.bottom.equalTo(self.view);
    }];
}

@end
